feat: add startup cleanup and maintenance system#16628
feat: add startup cleanup and maintenance system#16628jmylchreest wants to merge 1 commit intoanomalyco:devfrom
Conversation
Add configurable cleanup module that runs on startup to manage database size and remove orphaned storage files. Fixes log cleanup bug where non-deterministic glob ordering could delete newest files instead of oldest. Changes: - New cleanup config stanza with session retention, storage sweep, and vacuum options - Fix log cleanup: sort by filename before slicing, align guard with slice count - Sweep orphaned storage files (session, message, part, todo, session_diff, project, snapshot) - Prune empty directories after sweep - Run SQLite VACUUM and WAL checkpoint on startup - Fix pre-existing path.sep bug in Storage.list() for cross-platform correctness - Defer cleanup 500ms and yield every 100 deletions to avoid blocking TUI
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found several related PRs that address similar concerns: Potential duplicates/related PRs:
Note: PR #16201 appears to be the most closely related—it covers VACUUM support and storage reclamation which are core features of PR #16628. You may want to verify whether this PR supersedes, complements, or duplicates that work. |
Related Issues & PRs
Directly addressed by this PR
Related PRs (overlapping scope)
Related issues (symptoms this PR helps with)
|
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Issue for this PR
Closes #14731
Related: #16101, #12960
Type of change
What does this PR do?
Three things:
1. Fixes the log cleanup bug (#14731)
Glob.scanreturns files in non-deterministic order (depends on filesystem and path-scurry internals). The existingfiles.slice(0, -10)could delete the newest files instead of oldest. Fixed by addingfiles.sort()(ISO-timestamp filenames sort chronologically) and aligning the guard (<= 5→<= maxCount) with the slice count. ThemaxCountis now configurable via config.2. Adds a startup cleanup module
Runs in the background (deferred 500ms, yields every 100 deletions) to avoid blocking the TUI. It does:
max_age_days)session,session_diff,message,part,todo,project) that have no matching DB record — these are legacy pre-SQLite migration files that were never cleaned upAll configurable via a
cleanupconfig stanza with sensible defaults (vacuum enabled, session retention disabled/opt-in, all storage categories swept).3. Fixes a cross-platform bug in
Storage.list()path.sepwas used to split glob results, but glob always returns forward slashes regardless of OS. Changed tosplit("/").In testing on a real installation, this reclaimed ~2.8GB (115k orphaned files → 3 files).
How did you verify your code works?
bun run dev -- --print-logs --log-level DEBUGand verified cleanup logs appear with correct countsScreenshots / recordings
Before/after on a real install:
Checklist